<?php

/**
 * Images Model Class
 */
class AW_Imagetest_Model_Image extends Mage_Core_Model_Abstract
{
    /**
     * Model of Image Servise Api
     * @var AW_Imagetest_Model_Api_Abstract
     */
    protected $_apiModel = null;

    /**
     * Image State Unknown
     */
    const IMAGE_STATE_NOPREVIEW = 0;

    /**
     * Image State Ready
     */
    const IMAGE_STATE_PREVIEW = 1;
    /**
     * Status Enabled
     */
    const IMAGE_STATUS_ENABLED = 1;

    /**
     * Status Disabled
     */
    const IMAGE_STATUS_DISABLED = 0;

    /**
     * Status Pending
     */
    const IMAGE_STATUS_PENDING = 2;

    /**
     * Status Processing
     */
    const IMAGE_STATUS_PROCESSING = 3;

    /**
     * Class constructor
     */
    protected function _construct()
    {
        $this->_init('imagetest/image');
    }    

    /**
     * Retrives statuses array
     * @return array
     */
    public function getStatusesArray()
    {
        $helper = Mage::helper('imagetest');
        return array(
            self::IMAGE_STATUS_ENABLED => $helper->__('Enabled'),
            self::IMAGE_STATUS_DISABLED => $helper->__('Disabled'),
            self::IMAGE_STATUS_PENDING => $helper->__('Pending'),
            self::IMAGE_STATUS_PROCESSING => $helper->__('Processing'),
        );
    }
    public function getStatesArray()
    {
        $helper = Mage::helper('imagetest');
        return array(
            self::IMAGE_STATE_NOPREVIEW => $helper->__('No'),
            self::IMAGE_STATE_PREVIEW => $helper->__('Yes'),
        );
    }

    /**
     * Load connector of current image service
     * @return AW_Imagetest_Model_Image
     */
    public function loadApiModel()
    {
        if ($this->getApiCode()){
            $this->_apiModel = Mage::getSingleton('imagetest/connector')->getApiModel($this->getApiCode());
        }
        return $this;
    }

    /**
     * Retrives Image Service Connector
     * @return AW_Imagetest_Model_Connector
     */
    public function getApiModel()
    {
        if (!$this->_apiModel){
            $this->loadApiModel();
        }
        return $this->_apiModel;
    }

    /**
     * Add store to stores of this model instanse
     * @param int|string|array|Mage_Core_Model_Store $storeId
     * @return AW_Imagetest_Model_Image
     */
    public function setStore($store)
    {
        if ($store && $store instanceof Mage_Core_Model_Store){
            $this->setStore($store->getId());
        } elseif (is_array($store)) {
            foreach($store as $id){
                $this->setStore($id);
            }
        } elseif ($store) {
            if (!$this->getStores()){
                $this->setStores(array($store));
            } else {
                $stores = $this->getStores();
                if (!in_array($store, $stores)){
                    $stores[] = $store;
                    $this->setStores($stores);
                }
            }
        }
        return $this;
    }

    /**
     * Load comment to resource model
     * @param Mage_Core_Model_Abstract $object
     * @return AW_Imagetest_Model_Mysql4_Image
     */
    public function loadComment()
    {
        $this->getResource()->loadComment($this);
        return $this;
    }

    /**
     * Load stores to resource model
     * @param Mage_Core_Model_Abstract $object
     * @return AW_Imagetest_Model_Mysql4_Image
     */
    public function loadStores()
    {
        $this->getResource()->loadStores($this);
        return $this;
    }

    /**
     * Retrives count of Views for current image
     * @return int
     */
    public function getViews()
    {
        if ($api = $this->getApiModel()){
            return $api->getViews($this->getApiImageId());
        }
        return 0;
    }

    /**
     * Retrives "Image Ready" flag
     * @return
     */
    public function getReady()
    {
        $apiModel = $this->getApiModel();
        $entry = $this->getApiModel()->getImageData($this->getApiImageId());
        if ($entry->getReady()){
            $this->setApiImageUrl($entry->getImageUrl());
            $this->setThumbnail($entry->getThumbnailUrl());
            $this->setTime($entry->getTime());
            $this->setState(self::IMAGE_STATE_READY);
            $this->save();
            return true;
        } elseif ($entry->getRejected() || $entry->getFailed() || $entry->getDeleted()) {
            $this->setApiImageUrl($entry->getImageUrl());
            $this->setThumbnail($entry->getThumbnailUrl());
            $this->setTime($entry->getTime());
            $this->setStatus(self::IMAGE_STATUS_DISABLED);
            $state = self::IMAGE_STATE_UNKNOWN;
            if ($entry->getRejected()){
                $state = self::IMAGE_STATE_REJECTED;
            } elseif ($entry->getFailed()) {
                $state = self::IMAGE_STATE_FAILED;
            } elseif ($entry->getDeleted()) {
                $state = self::IMAGE_STATE_DELETED;
            }            
            $this->setState($state);
            $this->save();
            return false;
        }
        return false;
    }   
}

